<?php
namespace app\api\controller;
use app\api\model\UserModel;
use app\api\validate\UserValidate;
use app\BaseController;

class User extends BaseController
{
    //注册
    public function register(){
        //接受以post方式提交的值
        $data= input('post.');
//halt($data);
        //验证（验证器）
        $validate=new UserValidate();
        $result=$validate->scene('register');
        if(! $result){
           return error('验证失败');
        }

        //查询是否注册过手机号
        $user = UserModel::where('mobile','=',$data['mobile'])->find();
        if($user){
            return error('该手机号已被注册');
        }

        //生成8位随机加密盐
        $salt = createRandStr(8);
        //md5+加密盐加密
        $password = md5($data['password'].$salt);
        $data['password'] = $password;

        //入库
        if(UserModel::create($data)){
            return success('注册成功');
        }else{
            return error('注册失败');
        }
    }
    //登录
    public function login(){
        //接受以post方式提交的值
        $data= input('post.');
        //验证（验证器）
        $validate=new UserValidate();
        $result=$validate->scene('register');
        if(! $result){
            return error($validate->geterror());
        }
        //查询是否注册过手机号
        $user = UserModel::where('mobile','=',$data['mobile'])->find();
        if($user){
            return error('该手机号已被注册');
        }
        //密码
        if( md5($data['password'].$user['salt'] )!= $user['password']){
            return error('密码错误');
        }else{
            return success('密码chenggong',[
                //jwt
                'token'=>signToken($user['id'],$user['mobile']),
            ]);
        }
    }
    public function sendSmsCode(){
    $mobile = input('mobile','');
        if(empty($mobile)){
            return error('该手机号...');
        }
    }
}